//////////////////////////////////////////////////////////////////////////////////
// Company          :    Joinsilicon
// Engineer         :    Jinci.Bi
// Last Date        :    2024/05/30 10:20
// Module Name      :    LPDDR5 C driver
// Target Devices   : 
// Tool Versions    :    
// Dependencies     : 
// Revision 0.01    :    File Created
//////////////////////////////////////////////////////////////////////////////////

#include "perh_reg_lpddr5_ss.h"
#include <math.h>
#include <stdlib.h>

unsigned int prdata_js_monitor = 0x0;
unsigned int polling_timeout = 0x0;
unsigned int wl = 0x0;
unsigned int rl_0 = 0x0;
unsigned int rl_1 = 0x0;
unsigned int rl_2 = 0x0;
unsigned int wl_mr = 0x0;
unsigned int rl_mr = 0x0;
unsigned int phy_dll_2x = 0x0;
unsigned int readadj = 0x0;
unsigned int maskdur_adj = 0x0;
unsigned int twckpst = 0x0;
unsigned int twckpre_static = 0x0;
unsigned int twckpre_toggle_rd = 0x0;
unsigned int twckenl_rd_0 = 0x0;
unsigned int twckenl_rd_1 = 0x0;
unsigned int twckenl_rd_2 = 0x0;
unsigned int twckenl_wr = 0x0;
unsigned int twckenl_fs = 0x0;
unsigned int pll_m = 0x0;
signed 	 int pll_k = 0x0;
unsigned int pll_p = 0x0;
unsigned int pll_s = 0x0;
//void DELAY(unsigned int delay_value)
//{
//	unsigned int delays = 0;
//	delays = delay_value;
//	while(delays--);
//}

void perh_reg_wr(unsigned int master, unsigned long long addr, unsigned int wdata, unsigned int strb)
{

	unsigned long long addr_temp_js_monitor;
	volatile unsigned int *REAL_ADDR;
	unsigned long long addr_temp_js_monitor1;
	volatile unsigned int *REAL_ADDR1;

	if (master == 1)
	{
		addr_temp_js_monitor = ((addr) | DDR_PHY_BASE0 | REG_BASE0);
	}
	else
	{
		addr_temp_js_monitor = addr | REG_BASE0;
	}
	REAL_ADDR = (volatile unsigned int *)(addr_temp_js_monitor);
	*REAL_ADDR = wdata;
	
	if (master == 1)
	{
		addr_temp_js_monitor1 = ((addr) | DDR_PHY_BASE1 | REG_BASE1);
	}
	else
	{
		addr_temp_js_monitor1 = addr | REG_BASE1;
	}
	REAL_ADDR1 = (volatile unsigned int *)(addr_temp_js_monitor1);
	*REAL_ADDR1 = wdata;
}

unsigned int perh_reg_rd(unsigned int master, unsigned long long addr)
{
	unsigned long long addr_temp_js_monitor;
	volatile unsigned int *REAL_ADDR;
	if (master == 1)
	{
		addr_temp_js_monitor = ((addr) | DDR_PHY_BASE0 | REG_BASE0);
	}
	else
	{
		addr_temp_js_monitor = addr | REG_BASE0;
	}
	REAL_ADDR = (volatile unsigned int *)(addr_temp_js_monitor);
	return *REAL_ADDR;
}

void pll_setting(unsigned int pll_fin, unsigned int target_freq, unsigned int bypass_mode)
{
	pll_direct(pll_fin,target_freq);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
	prdata_js_monitor = prdata_js_monitor & 0xffffff7f;//reset the pll reset
	perh_reg_wr(0x00000000,0x00800028,prdata_js_monitor,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00804000);
	if(bypass_mode == 0x1)
	{
		prdata_js_monitor = (prdata_js_monitor & 0xffefffff);//reset bypass mode
		perh_reg_wr(0x00000000,0x00804000,prdata_js_monitor,0xf);
		prdata_js_monitor = prdata_js_monitor & 0xfff80000;
		prdata_js_monitor = (prdata_js_monitor | 0x00100000);//set bypass mode
		prdata_js_monitor = (prdata_js_monitor | ((pll_s & 0x7)<<0x10) | (pll_p & 0x3f));//set s and p
		prdata_js_monitor = (prdata_js_monitor | ((pll_m & 0x3ff) << 6));//set m
		perh_reg_wr(0x00000000,0x00804000,prdata_js_monitor,0xf);//set bypass mode
		perh_reg_wr(0x00000000,0x00804004,(pll_k & 0x0000ffff),0xf);
	}
	else
	{
  		prdata_js_monitor = (prdata_js_monitor & 0xffefffff);//reset bypass mode
		perh_reg_wr(0x00000000,0x00804000,prdata_js_monitor,0xf);//reset bypass mode
		prdata_js_monitor = prdata_js_monitor & 0xfff80000;
		prdata_js_monitor = (prdata_js_monitor | ((pll_s & 0x7)<<0x10) | (pll_p & 0x3f));//set s and p
		prdata_js_monitor = (prdata_js_monitor | ((pll_m & 0x3ff) << 6));//set m
		perh_reg_wr(0x00000000,0x00804000,prdata_js_monitor,0xf);//set bypass mode
		perh_reg_wr(0x00000000,0x00804004,(pll_k & 0x0000ffff),0xf);
	}
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
	prdata_js_monitor = prdata_js_monitor | 0x00000080;
	perh_reg_wr(0x00000000,0x00800028,prdata_js_monitor,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00804010);
	while(!((prdata_js_monitor & 0x00000001) == 0x00000001))
	{
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00804010);
		//while(1);
	}
}

void lpddr5_lf_initial(unsigned int pll_fin, unsigned int target_freq, unsigned int pll_pass){
	perh_reg_wr(0x00000000,0x00800028,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x0000ffff,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x00000000,0xf);
	pll_setting(pll_fin,target_freq,pll_pass);
	perh_reg_wr(0x00000000,0x00800028,0x000000a2,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x000000aa,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x000000ea,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x000000f8,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x000000fe,0xf);
}

void lpddr5_hf_initial(unsigned int pll_fin, unsigned int target_freq, unsigned int pll_pass, unsigned int phy_training){
	perh_reg_wr(0x00000000,0x00800028,0x000000be,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x000000ae,0xf);
	pll_setting(pll_fin,target_freq,pll_pass);
	lpddr5_freq_wl_rl_trans(target_freq);
	if(phy_training == 0x1){
		prdata_js_monitor = (0x46005000 | (rl_2 & 0x3f));
	}else{
		prdata_js_monitor = (0x46005000 | (rl_1 & 0x3f));
	}
	perh_reg_wr(0x00000001,0x00000000,prdata_js_monitor,0xf);
	prdata_js_monitor = (0x0000000c | (phy_dll_2x << 4));
	perh_reg_wr(0x00000001,0x00000a94,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x000000be,0xf);
	perh_reg_wr(0x00000000,0x00800028,0x000000fe,0xf);
	if(phy_training == 0x0){
		lpddr5_freq_wl_rl_trans(target_freq);
		prdata_js_monitor = (0x42000000 | ((readadj & 0xf) << 12) | ((maskdur_adj & 0x3) << 8));
		perh_reg_wr(0x00000001,0x0000000c,prdata_js_monitor,0xf);
		perh_reg_wr(0x00000001,0x00000018,0x028c4340,0xf);
		prdata_js_monitor = (0x008000c0 | (wl & 0x3f));
		perh_reg_wr(0x00000001,0x00001054,prdata_js_monitor,0xf);
		perh_reg_wr(0x00000001,0x00001058,prdata_js_monitor,0xf);
		perh_reg_wr(0x00000001,0x00000030,0x00f000f0,0xf);
		if(target_freq <= 3200){
			prdata_js_monitor = 0x00200020 | (twckenl_wr & 0xf) | ((twckenl_rd_0 & 0xf) << 8) | ((twckpre_toggle_rd & 0xf) << 12) | ((twckpre_static & 0xf) << 16);
		}else{
			prdata_js_monitor = 0x00200020 | (twckenl_wr & 0xf) | ((twckenl_rd_1 & 0xf) << 8) | ((twckpre_toggle_rd & 0xf) << 12) | ((twckpre_static & 0xf) << 16);
		}
		perh_reg_wr(0x00000001,0x00000adc,prdata_js_monitor,0xf);
		if(target_freq <= 3200){
			perh_reg_wr(0x00000001,0x00000004,0x581086c0,0xf);
		}
		if(target_freq <= 1067){
			perh_reg_wr(0x00000001,0x000000b0,0x2000017f,0xf);
		}
		if(target_freq <= 4267){
			perh_reg_wr(0x00000001,0x00001048,0x4c500000,0xf);
		}else{
			perh_reg_wr(0x00000001,0x00001048,0x4c000000,0xf);
		}
	}
}

void lpddr5_ctrl_static_reg_config(unsigned int phy_training, unsigned int target_freq){
	if(target_freq <= 3200){
		lpddr5_ctrl_static_reg_config_lf(phy_training, target_freq);
	}else{
		lpddr5_ctrl_static_reg_config_hf(phy_training, target_freq);
	}
}

void lpddr5_ctrl_initial_complete(){
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010c84);
	while(!(prdata_js_monitor == 0x00000001)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010c84);
		//while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010514);
	while(!(prdata_js_monitor == 0x00000001)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010514);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010c80,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010510,0x00000014,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000000,0xf);
	DELAY(         10);
	perh_reg_wr(0x00000000,0x00010510,0x00000015,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000001,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010c84);
	while(!(prdata_js_monitor == 0x00000001)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010c84);
		//while(1);
	}
}
void lpddr5_ctrl_static_reg_config_lf(unsigned int phy_training, unsigned int target_freq){
	lpddr5_freq_wl_rl_trans(target_freq);
	perh_reg_wr(0x00000000,0x00010000,0x03080008,0xf);
	perh_reg_wr(0x00000000,0x00010510,0x00000014,0xf);
	perh_reg_wr(0x00000000,0x00010518,0x80000001,0xf);
	perh_reg_wr(0x00000000,0x00010208,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010200,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010280,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010288,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010380,0x80012014,0xf);
	perh_reg_wr(0x00000000,0x00010100,0x00000020,0xf);
	perh_reg_wr(0x00000000,0x00010104,0x00000003,0xf);
	perh_reg_wr(0x00000000,0x00010108,0x00000003,0xf);
	perh_reg_wr(0x00000000,0x00010118,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010c90,0x00004001,0xf);
	perh_reg_wr(0x00000000,0x00010600,0x013f7f40,0xf);
	perh_reg_wr(0x00000000,0x00010604,0x00000fb2,0xf);
	perh_reg_wr(0x00000000,0x00010648,0x010008b8,0xf);
	perh_reg_wr(0x00000000,0x0001064c,0x02011e5c,0xf);
	perh_reg_wr(0x00000000,0x00010658,0x49f3b02d,0xf);
	perh_reg_wr(0x00000000,0x00010660,0x0000004f,0xf);
	perh_reg_wr(0x00000000,0x00010b80,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010650,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010300,0x00400040,0xf);
	perh_reg_wr(0x00000000,0x00010384,0x00002000,0xf);
	perh_reg_wr(0x00000000,0x0001038c,0x04040208,0xf);
	perh_reg_wr(0x00000000,0x00010390,0x08400810,0xf);
	perh_reg_wr(0x00000000,0x00010394,0x10000204,0xf);
	perh_reg_wr(0x00000000,0x00010500,0x00110011,0xf);
	perh_reg_wr(0x00000000,0x00010508,0x20008000,0xf);
	perh_reg_wr(0x00000000,0x00010ca0,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010f00,0x80008200,0xf);
	perh_reg_wr(0x00000000,0x00010580,0x00110011,0xf);
	perh_reg_wr(0x00000000,0x00010ca4,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010308,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x0001018c,0x0000003f,0xf);
	perh_reg_wr(0x00000000,0x00010184,0x00000002,0xf);
	perh_reg_wr(0x00000000,0x00010114,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010124,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010c94,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010b84,0x00000003,0xf);
	perh_reg_wr(0x00000000,0x00010180,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x0001060c,0x00000700,0xf);
	perh_reg_wr(0x00000000,0x00010d00,0x4002004e,0xf);
	perh_reg_wr(0x00000000,0x00010010,0x00000101,0xf);
	perh_reg_wr(0x00000000,0x00010084,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010284,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010984,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010988,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010990,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010b8c,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010b98,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000005a8,0x01900003,0xf);
	perh_reg_wr(0x00000000,0x000005a0,0x00000202,0xf);
	perh_reg_wr(0x00000000,0x000005a4,0x00000200,0xf);
	perh_reg_wr(0x00000000,0x000005b0,0x00000004,0xf);
	perh_reg_wr(0x00000000,0x00000d00,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x000005b4,0xc000012c,0xf);
	perh_reg_wr(0x00000000,0x00000000,0x29101b22,0xf);
	perh_reg_wr(0x00000000,0x00000004,0x05060630,0xf);
	if(phy_training == 0x1){
		prdata_js_monitor = (0x00001219 | ((wl & 0x7f) << 24) | ((rl_2 & 0x7f) << 16));
	}else{
		prdata_js_monitor = (0x00001219 | ((wl & 0x7f) << 24) | ((rl_1 & 0x7f) << 16));
	}
	perh_reg_wr(0x00000000,0x00000008,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x0000000c,0x000c202e,0xf);
	perh_reg_wr(0x00000000,0x00000010,0x0f02040f,0xf);
	perh_reg_wr(0x00000000,0x00000014,0x02040c01,0xf);
	perh_reg_wr(0x00000000,0x00000018,0x00000008,0xf);
	perh_reg_wr(0x00000000,0x0000001c,0x00000003,0xf);
	perh_reg_wr(0x00000000,0x00000024,0x00020412,0xf);
	perh_reg_wr(0x00000000,0x00000030,0x00030000,0xf);
	perh_reg_wr(0x00000000,0x00000034,0x0c080002,0xf);
	perh_reg_wr(0x00000000,0x00000038,0x002000e6,0xf);
	perh_reg_wr(0x00000000,0x00000060,0x0210160c,0xf);
	perh_reg_wr(0x00000000,0x00000064,0x00002a06,0xf);
	perh_reg_wr(0x00000000,0x00000078,0x00181217,0xf);
	perh_reg_wr(0x00000000,0x00000080,0x00030408,0xf);
	prdata_js_monitor = (0x02100000 | ((int)(3.906 * target_freq/256) & 0xffff));
	perh_reg_wr(0x00000000,0x00000600,prdata_js_monitor,0xf);
	prdata_js_monitor = ((280 * target_freq /8000) & 0xfff);
	perh_reg_wr(0x00000000,0x00000604,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00000608,0x06480000,0xf);
	perh_reg_wr(0x00000000,0x0000060c,0x10000000,0xf);
	perh_reg_wr(0x00000000,0x00000610,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00000800,0x0018064a,0xf);
	perh_reg_wr(0x00000000,0x00000804,0x02800100,0xf);
	perh_reg_wr(0x00000000,0x00000d0c,0x00400010,0xf);
	perh_reg_wr(0x00000000,0x00000c84,0x0f00007f,0xf);
	perh_reg_wr(0x00000000,0x00000b80,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00000b04,0x1024100a,0xf);
	perh_reg_wr(0x00000000,0x00000b08,0x00000533,0xf);
	perh_reg_wr(0x00000000,0x00000b00,0x00800000,0xf);
	perh_reg_wr(0x00000000,0x00000d04,0x00000c10,0xf);
	if(phy_training == 0x1){
		prdata_js_monitor = (0x03000800 | (((rl_2+7)*4) << 16) | (wl*4-3));
	}else{
		prdata_js_monitor = (0x03000800 | (((rl_1+7)*4) << 16) | (wl*4-2));
	}
	perh_reg_wr(0x00000000,0x00000580,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00000584,0x00100303,0xf);
	perh_reg_wr(0x00000000,0x00000588,0x00183f22,0xf);
	if(phy_training == 0x1){
		prdata_js_monitor = (0x00000003 | ((twckenl_fs * 4 +2) << 8) | ((twckenl_wr * 4 +2) << 16) | ((twckenl_rd_1 * 4 +2) << 24));
	}else{
		prdata_js_monitor = (0x00000003 | ((twckenl_fs * 4 +2) << 8) | ((twckenl_wr * 4 +2) << 16) | ((twckenl_rd_0 * 4 +2) << 24));
	}
	perh_reg_wr(0x00000000,0x00000590,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00000594,0x04040007,0xf);
	perh_reg_wr(0x00000000,0x00000598,0x0000010f,0xf);
	perh_reg_wr(0x00000000,0x00000500,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00000504,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00000508,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x0000050c,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x0000005c,0x009d0000,0xf);
	perh_reg_wr(0x00000000,0x00000c00,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000005ac,0x00010001,0xf);
	perh_reg_wr(0x00000000,0x00000a80,0x00000070,0xf);
	perh_reg_wr(0x00000000,0x00000d08,0x00001408,0xf);
	perh_reg_wr(0x00000000,0x00000c80,0x0f000001,0xf);
	perh_reg_wr(0x00000000,0x00000c88,0x0f00007f,0xf);
	perh_reg_wr(0x00000000,0x00000650,0x00000098,0xf);
	perh_reg_wr(0x00000000,0x00000d80,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00020000,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00020004,0x0000101f,0xf);
	perh_reg_wr(0x00000000,0x0002f008,0x0000501f,0xf);
	perh_reg_wr(0x00000000,0x00020090,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00020094,0x02000e00,0xf);
	perh_reg_wr(0x00000000,0x000200e0,0x1000ff10,0xf);
	perh_reg_wr(0x00000000,0x000200e8,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000200f0,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000200f4,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000200f8,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000200fc,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00030030,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00030004,0x00001f1f,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x0003000c,0x003f0903,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x00030010,0x00000101,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x00030014,0x1f030303,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x00030018,0x03030300,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x0003001c,0x1f080808,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x00030020,0x08080808,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x00030024,0x08080808,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x00030028,0x08080808,0xf);//modified by jinci 2024.07.19 for address mapping
	perh_reg_wr(0x00000000,0x0003002c,0x00000808,0xf);//modified by jinci 2024.07.19 for address mapping
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010b84);
	while(!(prdata_js_monitor == 0x00000003)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010b84);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010b84,0x00000002,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010d00);
	while(!(prdata_js_monitor == 0x4002004e)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010d00);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010d00,0x4002004e,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
	while(!(prdata_js_monitor == 0x000000fe)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00800028,0x000000ff,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
	while(!(prdata_js_monitor == 0x000000ff)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00800028,0x000000ff,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010510,0x00000034,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000001,0xf);
}

void lpddr5_ctrl_static_reg_config_hf(unsigned int phy_training, unsigned int target_freq){
	lpddr5_freq_wl_rl_trans(target_freq);
	perh_reg_wr(0x00000000,0x00010000,0x03080008,0xf);
	perh_reg_wr(0x00000000,0x00010510,0x00000014,0xf);
	perh_reg_wr(0x00000000,0x00010518,0x80000001,0xf);
	perh_reg_wr(0x00000000,0x00010208,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010200,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010280,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010288,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010380,0x80012014,0xf);
	perh_reg_wr(0x00000000,0x00010100,0x00000020,0xf);
	perh_reg_wr(0x00000000,0x00010104,0x00000003,0xf);
	perh_reg_wr(0x00000000,0x00010108,0x00000003,0xf);
	perh_reg_wr(0x00000000,0x00010118,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010c90,0x00004001,0xf);
	perh_reg_wr(0x00000000,0x00010600,0x013f7f40,0xf);
	perh_reg_wr(0x00000000,0x00010604,0x00000fb2,0xf);
	perh_reg_wr(0x00000000,0x00010648,0x010008b8,0xf);
	perh_reg_wr(0x00000000,0x0001064c,0x02011e5c,0xf);
	perh_reg_wr(0x00000000,0x00010658,0x49f3b02d,0xf);
	perh_reg_wr(0x00000000,0x00010660,0x0000004f,0xf);
	perh_reg_wr(0x00000000,0x00010b80,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010650,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010300,0x00400040,0xf);
	perh_reg_wr(0x00000000,0x00010384,0x00002000,0xf);
	perh_reg_wr(0x00000000,0x0001038c,0x04040208,0xf);
	perh_reg_wr(0x00000000,0x00010390,0x08400810,0xf);
	perh_reg_wr(0x00000000,0x00010394,0x10000204,0xf);
	perh_reg_wr(0x00000000,0x00010500,0x00110011,0xf);
	perh_reg_wr(0x00000000,0x00010508,0x20008000,0xf);
	perh_reg_wr(0x00000000,0x00010ca0,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010f00,0x80008200,0xf);
	perh_reg_wr(0x00000000,0x00010580,0x00110011,0xf);
	perh_reg_wr(0x00000000,0x00010ca4,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010308,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x0001018c,0x0000003f,0xf);
	perh_reg_wr(0x00000000,0x00010184,0x00000002,0xf);
	perh_reg_wr(0x00000000,0x00010114,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010124,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010c94,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x00010b84,0x00000003,0xf);
	perh_reg_wr(0x00000000,0x00010180,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x0001060c,0x00000700,0xf);
	perh_reg_wr(0x00000000,0x00010d00,0x4002004e,0xf);
	perh_reg_wr(0x00000000,0x00010010,0x00000101,0xf);
	perh_reg_wr(0x00000000,0x00010084,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010284,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010984,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010988,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010990,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010b8c,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010b98,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000005a8,0x01900003,0xf);
	perh_reg_wr(0x00000000,0x000005a0,0x00000202,0xf);
	perh_reg_wr(0x00000000,0x000005a4,0x00000200,0xf);
	perh_reg_wr(0x00000000,0x000005b0,0x00000004,0xf);
	perh_reg_wr(0x00000000,0x00000d00,0x00000001,0xf);
	perh_reg_wr(0x00000000,0x000005b4,0xc000012c,0xf);
	perh_reg_wr(0x00000000,0x00000000,0x29101b22,0xf);
	perh_reg_wr(0x00000000,0x00000004,0x05060630,0xf);
	if(phy_training == 0x1){
		prdata_js_monitor = (0x00001219 | ((wl & 0x7f) << 24) | ((rl_2 & 0x7f) << 16));
	}else{
		prdata_js_monitor = (0x00001219 | ((wl & 0x7f) << 24) | ((rl_1 & 0x7f) << 16));
	}
	perh_reg_wr(0x00000000,0x00000008,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x0000000c,0x000c2230,0xf);
	perh_reg_wr(0x00000000,0x00000010,0x0f04040f,0xf);
	perh_reg_wr(0x00000000,0x00000014,0x02040c01,0xf);
	perh_reg_wr(0x00000000,0x00000018,0x00000008,0xf);
	perh_reg_wr(0x00000000,0x0000001c,0x00000003,0xf);
	perh_reg_wr(0x00000000,0x00000024,0x00020414,0xf);
	perh_reg_wr(0x00000000,0x00000030,0x00030000,0xf);
	perh_reg_wr(0x00000000,0x00000034,0x0c100002,0xf);
	perh_reg_wr(0x00000000,0x00000038,0x002000e6,0xf);
	perh_reg_wr(0x00000000,0x00000060,0x0010180e,0xf);
	perh_reg_wr(0x00000000,0x00000064,0x00002a06,0xf);
	perh_reg_wr(0x00000000,0x00000078,0x001a1419,0xf);
	perh_reg_wr(0x00000000,0x00000080,0x00030408,0xf);
	prdata_js_monitor = (0x02100000 | ((int)(3.906 * target_freq/256) & 0xffff));
	perh_reg_wr(0x00000000,0x00000600,prdata_js_monitor,0xf);
	prdata_js_monitor = ((280 * target_freq /8000) & 0xfff);
	perh_reg_wr(0x00000000,0x00000604,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00000608,0x06480000,0xf);
	perh_reg_wr(0x00000000,0x0000060c,0x10000000,0xf);
	perh_reg_wr(0x00000000,0x00000610,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00000800,0x0018064a,0xf);
	perh_reg_wr(0x00000000,0x00000804,0x02800100,0xf);
	perh_reg_wr(0x00000000,0x00000d0c,0x00400010,0xf);
	perh_reg_wr(0x00000000,0x00000c84,0x0f00007f,0xf);
	perh_reg_wr(0x00000000,0x00000b80,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00000b04,0x1024100a,0xf);
	perh_reg_wr(0x00000000,0x00000b08,0x00000533,0xf);
	perh_reg_wr(0x00000000,0x00000b00,0x00800000,0xf);
	perh_reg_wr(0x00000000,0x00000d04,0x00000e12,0xf);
	if(phy_training == 0x1){
		prdata_js_monitor = (0x03000800 | (((rl_2+7)*4) << 16) | (wl*4-3));
	}else{
		prdata_js_monitor = (0x03000800 | (((rl_1+7)*4) << 16) | (wl*4-2));
	}
	perh_reg_wr(0x00000000,0x00000580,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00000584,0x00100303,0xf);
	perh_reg_wr(0x00000000,0x00000588,0x00183f22,0xf);
	if(phy_training == 0x1){
		prdata_js_monitor = (0x00000003 | ((twckenl_fs * 4 +2) << 8) | ((twckenl_wr * 4 +2) << 16) | ((twckenl_rd_2 * 4 +2) << 24));
	}else{
		prdata_js_monitor = (0x00000003 | ((twckenl_fs * 4 +2) << 8) | ((twckenl_wr * 4 +2) << 16) | ((twckenl_rd_1 * 4 +2) << 24));
	}
	perh_reg_wr(0x00000000,0x00000590,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00000594,0x0410000f,0xf);
	perh_reg_wr(0x00000000,0x00000598,0x00000117,0xf);
	perh_reg_wr(0x00000000,0x00000500,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00000504,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00000508,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x0000050c,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x0000005c,0x009d0000,0xf);
	perh_reg_wr(0x00000000,0x00000c00,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000005ac,0x00010001,0xf);
	perh_reg_wr(0x00000000,0x00000a80,0x00000070,0xf);
	perh_reg_wr(0x00000000,0x00000d08,0x0000160a,0xf);
	perh_reg_wr(0x00000000,0x00000c80,0x0f000001,0xf);
	perh_reg_wr(0x00000000,0x00000c88,0x0f00007f,0xf);
	perh_reg_wr(0x00000000,0x00000650,0x00000098,0xf);
	perh_reg_wr(0x00000000,0x00000d80,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00020000,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00020004,0x0000101f,0xf);
	perh_reg_wr(0x00000000,0x0002f008,0x0000501f,0xf);
	perh_reg_wr(0x00000000,0x00020090,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00020094,0x02000e00,0xf);
	perh_reg_wr(0x00000000,0x000200e0,0x1000ff10,0xf);
	perh_reg_wr(0x00000000,0x000200e8,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000200f0,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000200f4,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000200f8,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x000200fc,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00030030,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00030004,0x0000001f,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x0003000c,0x003f0903,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x00030010,0x00000101,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x00030014,0x1f030303,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x00030018,0x03030300,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x0003001c,0x1f080808,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x00030020,0x08080808,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x00030024,0x08080808,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x00030028,0x08080808,0xf);//modified by jinci 2024.07.23 for address mapping
	perh_reg_wr(0x00000000,0x0003002c,0x00000808,0xf);//modified by jinci 2024.07.23 for address mapping
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010b84);
	while(!(prdata_js_monitor == 0x00000003)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010b84);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010b84,0x00000002,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010d00);
	while(!(prdata_js_monitor == 0x4002004e)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010d00);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010d00,0x4002004e,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
	while(!(prdata_js_monitor == 0x000000fe)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00800028,0x000000ff,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
	while(!(prdata_js_monitor == 0x000000ff)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00800028);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00800028,0x000000ff,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010510,0x00000034,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000001,0xf);
}

void lpddr5_mrw_config(unsigned int target_freq){
	lpddr5_freq_wl_rl_trans(target_freq);
	lpddr5_mrw(0x12,0x19);
	prdata_js_monitor = (wl_mr << 4);
	lpddr5_mrw(0x1,prdata_js_monitor);
	prdata_js_monitor = (rl_mr | (rl_mr << 4));
	lpddr5_mrw(0x2,prdata_js_monitor);
	if(target_freq <= 3200){
		lpddr5_mrw(0x3,0x16);
	}else{
		lpddr5_mrw(0x3,0x6);
	}
	lpddr5_mrw(0xa,0x49);
	lpddr5_mrw(0xb,0x14);
	lpddr5_mrw(0xc,0x22);
	lpddr5_mrw(0xd,0x1);
	lpddr5_mrw(0xe,0x12);
	lpddr5_mrw(0xf,0x12);
	lpddr5_mrw(0x11,0x3e);
	lpddr5_mrw(0x13,0x0);
	lpddr5_mrw(0x14,0x2);
	lpddr5_mrw(0x16,0x0);
	lpddr5_mrw(0x17,0x0);
	lpddr5_mrw(0x19,0x0);
	lpddr5_mrw(0x1c,0xc);
	lpddr5_mrw(0x25,0x40);
	lpddr5_mrw(0x28,0x40);
	lpddr5_mrw(0x29,0x40);
	lpddr5_mrw(0x2e,0x0);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010014);
	while(!(prdata_js_monitor == 0x00000001)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010014);
		//while(1);
	}
}

void lpddr5_ctrl_dynamic_reg_config(){
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010014);
	while(!(prdata_js_monitor == 0x00000001)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010014);
		//while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010508);
	while(!(prdata_js_monitor == 0x20008000)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010508);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010c80,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010508,0xa0008000,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000001,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010c84);
	while(!(prdata_js_monitor == 0x00000001)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010c84);
		//while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010280);
	while(!(prdata_js_monitor == 0x00000000)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010280);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010280,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010288);
	while(!(prdata_js_monitor == 0x00000000)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010288);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010288,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00010c80,0x00000001,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010c84);
	while(!(prdata_js_monitor == 0x00000001)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010c84);
		//while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010208);
	while(!(prdata_js_monitor == 0x00000000)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010208);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010208,0x00000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010180);
	while(!(prdata_js_monitor == 0x00000000)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010180);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010180,0x00000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010b84);
	while(!(prdata_js_monitor == 0x00000002)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010b84);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010b84,0x00000000,0xf);
	perh_reg_wr(0x00000000,0x00020090,0x00000001,0xf);
}

void lpddr5_mrw(unsigned int mr_addr,unsigned int mr_value){
	perh_reg_wr(0x00000000,0x00010080,0x00000010,0xf);
	prdata_js_monitor = (((mr_addr & 0xff) << 8) | (mr_value & 0xff));
	perh_reg_wr(0x00000000,0x00010084,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00010080,0x80000010,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010090);
	while(!(prdata_js_monitor == 0x00000000)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010090);
		//while(1);
	}
}

unsigned int lpddr5_mrr(unsigned int mr_addr){
	perh_reg_wr(0x00000000,0x00010080,0x00000011,0xf);
	prdata_js_monitor = ((mr_addr & 0xff) << 8);
	perh_reg_wr(0x00000000,0x00010084,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000000,0x00010080,0x80000011,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010090);
	while(!(prdata_js_monitor == 0x00010000)){
		prdata_js_monitor = perh_reg_rd(0x00000000,0x00010090);
		//while(1);
	}
	perh_reg_wr(0x00000000,0x00010080,0x01000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000000,0x00010094);
	return prdata_js_monitor;
}

void lpddr5_freq_wl_rl_trans(unsigned int target_freq){
	switch(target_freq){
		case 40 ... 533:
			wl = 2; rl_0 = 3; rl_1 = 3; rl_2 = 3; wl_mr = 0; rl_mr = 0; phy_dll_2x = 1; readadj = 10; maskdur_adj = 3; twckpst = 0; twckpre_static = 1; twckpre_toggle_rd = 3; twckenl_rd_0 = 0; twckenl_rd_1 = 0; twckenl_rd_2 = 0; twckenl_wr = 0; twckenl_fs = 0;
			break;
		case 534 ... 1067:
			wl = 2; rl_0 = 4; rl_1 = 4; rl_2 = 4; wl_mr = 1; rl_mr = 1; phy_dll_2x = 1; readadj = 10; maskdur_adj = 3; twckpst = 0; twckpre_static = 1; twckpre_toggle_rd = 4; twckenl_rd_0 = 0; twckenl_rd_1 = 0; twckenl_rd_2 = 0; twckenl_wr = 0; twckenl_fs = 0;
			break;
		case 1068 ... 1600:
			wl = 3; rl_0 = 5; rl_1 = 5; rl_2 = 6; wl_mr = 2; rl_mr = 2; phy_dll_2x = 1; readadj = 10; maskdur_adj = 3; twckpst = 0; twckpre_static = 1; twckpre_toggle_rd = 4; twckenl_rd_0 = 1; twckenl_rd_1 = 1; twckenl_rd_2 = 2; twckenl_wr = 1; twckenl_fs = 1;
			break;
		case 1601 ... 2133:
			wl = 4; rl_0 = 6; rl_1 = 7; rl_2 = 7; wl_mr = 3; rl_mr = 3; phy_dll_2x = 1; readadj = 10; maskdur_adj = 3; twckpst = 2; twckpre_static = 2; twckpre_toggle_rd = 4; twckenl_rd_0 = 1; twckenl_rd_1 = 2; twckenl_rd_2 = 2; twckenl_wr = 1; twckenl_fs = 1;
			break;
		case 2134 ... 2750:
			wl = 4; rl_0 = 8; rl_1 = 8; rl_2 = 9; wl_mr = 4; rl_mr = 4; phy_dll_2x = 1; readadj = 9; maskdur_adj = 2; twckpst = 2; twckpre_static = 2; twckpre_toggle_rd = 5; twckenl_rd_0 = 2; twckenl_rd_1 = 2; twckenl_rd_2 = 3; twckenl_wr = 1; twckenl_fs = 1;
			break;
		case 2751 ... 3200:
			wl = 5; rl_0 = 9; rl_1 = 10; rl_2 = 10; wl_mr = 5; rl_mr = 5; phy_dll_2x = 1; readadj = 9; maskdur_adj = 2; twckpst = 2; twckpre_static = 2; twckpre_toggle_rd = 5; twckenl_rd_0 = 3; twckenl_rd_1 = 4; twckenl_rd_2 = 4; twckenl_wr = 2; twckenl_fs = 1;
			break;
		case 3201 ... 3733:
			wl = 6; rl_0 = 10; rl_1 = 11; rl_2 = 12; wl_mr = 6; rl_mr = 6; phy_dll_2x = 0; readadj = 0; maskdur_adj = 0; twckpst = 2; twckpre_static = 3; twckpre_toggle_rd = 5; twckenl_rd_0 = 3; twckenl_rd_1 = 4; twckenl_rd_2 = 5; twckenl_wr = 2; twckenl_fs = 1;
			break;
		case 3734 ... 4267:
			wl = 6; rl_0 = 12; rl_1 = 13; rl_2 = 14; wl_mr = 7; rl_mr = 7; phy_dll_2x = 0; readadj = 0; maskdur_adj = 0; twckpst = 2; twckpre_static = 3; twckpre_toggle_rd = 6; twckenl_rd_0 = 4; twckenl_rd_1 = 5; twckenl_rd_2 = 6; twckenl_wr = 2; twckenl_fs = 1;
			break;
		case 4268 ... 4800:
			wl = 7; rl_0 = 13; rl_1 = 14; rl_2 = 15; wl_mr = 8; rl_mr = 8; phy_dll_2x = 0; readadj = 0; maskdur_adj = 0; twckpst = 2; twckpre_static = 3; twckpre_toggle_rd = 6; twckenl_rd_0 = 5; twckenl_rd_1 = 6; twckenl_rd_2 = 7; twckenl_wr = 3; twckenl_fs = 2;
			break;
		case 4801 ... 5500:
			wl = 8; rl_0 = 15; rl_1 = 16; rl_2 = 17; wl_mr = 9; rl_mr = 9; phy_dll_2x = 0; readadj = 0; maskdur_adj = 0; twckpst = 2; twckpre_static = 4; twckpre_toggle_rd = 6; twckenl_rd_0 = 6; twckenl_rd_1 = 7; twckenl_rd_2 = 8; twckenl_wr = 3; twckenl_fs = 2;
			break;
		case 5501 ... 6000:
			wl = 9; rl_0 = 16; rl_1 = 17; rl_2 = 19; wl_mr = 10; rl_mr = 10; phy_dll_2x = 0; readadj = 0; maskdur_adj = 0; twckpst = 2; twckpre_static = 4; twckpre_toggle_rd = 7; twckenl_rd_0 = 6; twckenl_rd_1 = 7; twckenl_rd_2 = 9; twckenl_wr = 4; twckenl_fs = 2;
			break;
		case 6001 ... 6400:
			wl = 9; rl_0 = 17; rl_1 = 18; rl_2 = 20; wl_mr = 11; rl_mr = 11; phy_dll_2x = 0; readadj = 0; maskdur_adj = 0; twckpst = 2; twckpre_static = 4; twckpre_toggle_rd = 7; twckenl_rd_0 = 7; twckenl_rd_1 = 8; twckenl_rd_2 = 10; twckenl_wr = 4; twckenl_fs = 2;
			break;
		case 6401 ... 7500:
			wl = 11; rl_0 = 20; rl_1 = 22; rl_2 = 24; wl_mr = 12; rl_mr = 12; phy_dll_2x = 0; readadj = 0; maskdur_adj = 0; twckpst = 2; twckpre_static = 5; twckpre_toggle_rd = 9; twckenl_rd_0 = 7; twckenl_rd_1 = 9; twckenl_rd_2 = 11; twckenl_wr = 5; twckenl_fs = 3;
			break;
		case 7501 ... 8533:
			wl = 12; rl_0 = 23; rl_1 = 25; rl_2 = 26; wl_mr = 13; rl_mr = 13; phy_dll_2x = 0; readadj = 0; maskdur_adj = 0; twckpst = 2; twckpre_static = 6; twckpre_toggle_rd = 10; twckenl_rd_0 = 8; twckenl_rd_1 = 10; twckenl_rd_2 = 11; twckenl_wr = 5; twckenl_fs = 3;
			break;
		default:
			wl = 2; rl_0 = 3; rl_1 = 3; rl_2 = 3; wl_mr = 0; rl_mr = 0; phy_dll_2x = 1; readadj = 12; maskdur_adj = 3; twckpst = 0; twckpre_static = 1; twckpre_toggle_rd = 3; twckenl_rd_0 = 0; twckenl_rd_1 = 0; twckenl_rd_2 = 0; twckenl_wr = 0; twckenl_fs = 0;
			break;
	}
}

void pll_direct(unsigned int pll_fin,unsigned int target_freq){
	if( (target_freq >= 4250) && (target_freq <= 8500) ){
		pll_m = target_freq / (2 * pll_fin);
		pll_s = 0;
	}else if( (target_freq >= 66) && (target_freq < 4250) ){
		for(int pll_mul = 0; pll_mul <= 6; pll_mul++){
			if( (target_freq * (1 << pll_mul) >= 4250) && (target_freq * (1 << pll_mul) <= 8500) ){
				pll_s = pll_mul;
				break;
			}
		}
		pll_m = target_freq * (1 << pll_s) / (2 * pll_fin);
	}
	if(pll_m < 64){
		pll_p = 64/pll_m + 1;
		pll_m = pll_m * pll_p;
	}else{
		pll_p = 1;
	}
	if((target_freq * pll_p * (1 << pll_s) - pll_m*2*pll_fin)*65536/2/pll_fin > 32768){
		pll_m = pll_m+1;
	}
	pll_k = (target_freq * pll_p * (1 << pll_s) - pll_m*2*pll_fin)*65536/2/pll_fin;
}			

void lpddr5_phy_training(unsigned int phy_training,unsigned int target_freq,unsigned int phy_hw_cbt,unsigned int phy_wrlvl,unsigned int phy_rd,unsigned int phy_wr,unsigned int phy_prbs_rd,unsigned int phy_prbs_wr){
	if(phy_training == 0x1){
		samsung_common_setting_before_training(target_freq);//zq calibration
		if(phy_hw_cbt == 0x1){
			samsung_hwcal_cbt(target_freq);
		}
		if(phy_prbs_rd | phy_prbs_wr){
			phy_wrlvl = 0x1;
			phy_rd = 0x1;
			phy_wr = 0x1;
		}
		if(phy_wrlvl == 0x1){
			samsung_wrlvl(target_freq);
		}
		samsung_vref_setting(target_freq); // vref setting
		if(phy_rd == 0x1){
			samsung_rd(target_freq);
		}
		if(phy_wr == 0x1){
			samsung_wr(target_freq);
		}
		if(phy_prbs_rd){
			samsung_prbs_rd(target_freq);
		}
		if(phy_prbs_wr){
			samsung_prbs_wr(target_freq);
		}
//		perh_reg_wr(0x00000001,0x00000030,0x00000250,0xf);// added by jinci
//		perh_reg_wr(0x00000001,0x0000003c,0x00003f80,0xf);// added by jinci
		perh_reg_wr(0x00000001,0x00000020,0x00a000a0,0xf);// added by jinci
//		perh_reg_wr(0x00000001,0x00000058,0x000500000,0xf);
//		perh_reg_wr(0x00000000,0x00000010,0xff043f7f,0xf);
//		perh_reg_wr(0x00000000,0x00000000,0x2910221b,0xf);
		
//		perh_reg_wr(0x00000001,0x00000050,0x200001ff,0xf);	
/*
//modify RL
		prdata_js_monitor = (0x00001219 | ((wl & 0x7f) << 24) | ((rl_1 & 0x7f) << 16));
		perh_reg_wr(0x00000000,0x00000008,prdata_js_monitor,0xf);
		prdata_js_monitor = (0x03000800 | (((rl_1+7)*4) << 16) | (wl*4 -8));
		perh_reg_wr(0x00000000,0x00000580,prdata_js_monitor,0xf);
		if(target_freq <= 3200){
			prdata_js_monitor = (0x00000003 | ((twckenl_fs * 4 +2) << 8) | ((twckenl_wr * 4 +2) << 16) | ((twckenl_rd_0 * 4 +2) << 24));
		}else{
			prdata_js_monitor = (0x00000003 | ((twckenl_fs * 4 +2) << 8) | ((twckenl_wr * 4 +2) << 16) | ((twckenl_rd_1 * 4 +2) << 24));
		}
		perh_reg_wr(0x00000000,0x00000590,prdata_js_monitor,0xf);
		prdata_js_monitor = (0x46005000 | (rl_1 & 0x3f));
		perh_reg_wr(0x00000001,0x00000000,prdata_js_monitor,0xf);
//set wck,Twckenl_wr(3:0) = 5,twckpre_static(19:16) = 5, twckpretoggle_wr(7:4) = 2,Twckenl_rd(11:8) = a->8, twckpre_static = 5,twckpretoggle_rd(15:12) =9,twckpst(21:20) = MR10[3:2]=6.5tWCK
		if(target_freq <= 3200){
			prdata_js_monitor = 0x00200020 | (twckenl_wr & 0xf) | ((twckenl_rd_0 & 0xf) << 8) | ((twckpre_toggle_rd & 0xf) << 12) | ((twckpre_static & 0xf) << 16);
		}else{
			prdata_js_monitor = 0x00200020 | (twckenl_wr & 0xf) | ((twckenl_rd_1 & 0xf) << 8) | ((twckpre_toggle_rd & 0xf) << 12) | ((twckpre_static & 0xf) << 16);
		}
		perh_reg_wr(0x00000001,0x00000adc,prdata_js_monitor,0xf);
//		perh_reg_wr(0x00000001,0x00000030,0x00f000f0,0xf);// added by jinci
		if(phy_wr == 0x0){
			perh_reg_wr(0x00000001,0x00000030,0x01150115,0xf);
		}
		perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
		if(target_freq <= 1067){
			perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
		}else{
			perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
		}
		perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		while(!(prdata_js_monitor == 0x2f000000)){
			prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
			//while(1);
		}
		perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
		if(target_freq <= 1067){
			perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
		}else{
			perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
		}
*/
	}
}

void samsung_vref_setting(unsigned target_freq){
        perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
        lpddr5_mrw(0xb,0x14);
        lpddr5_mrw(0xc,0x22);
        lpddr5_mrw(0x11,0x3e);
        perh_reg_wr(0x00000001,0x000003ec,0x00001f1f,0xf);
        perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
        perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
        perh_reg_wr(0x00000001,0x00000e80,0x21212121,0xf);
        perh_reg_wr(0x00000001,0x00000e7c,0x3f3f3f3f,0xf);
        lpddr5_mrw(0xe,0x12);
        lpddr5_mrw(0xf,0x12);
        lpddr5_mrw(0xb,0x14);
}

void samsung_common_setting_before_training(unsigned int target_freq){
	lpddr5_freq_wl_rl_trans(target_freq);
	perh_reg_wr(0x00000001,0x00000160,0x00003000,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x42001300,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4400,0xf);
	prdata_js_monitor = (0x46005000 | (rl_2 & 0x3f));
	perh_reg_wr(0x00000001,0x00000000,prdata_js_monitor,0xf);
	prdata_js_monitor = (0x008000c0 | (wl & 0x3f));
	perh_reg_wr(0x00000001,0x00001054,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000014,0x0083806f,0xf);
	perh_reg_wr(0x00000001,0x00000df8,0x59200016,0xf);
	perh_reg_wr(0x00000001,0x00000dfc,0x59200016,0xf);
	perh_reg_wr(0x00000001,0x00000e00,0x59200016,0xf);
	prdata_js_monitor = (0x008000c0 | (wl & 0x3f));
	perh_reg_wr(0x00000001,0x00001054,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00001058,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x0000105c,prdata_js_monitor,0xf);
	prdata_js_monitor = (0x0000000c | (phy_dll_2x << 4));
	perh_reg_wr(0x00000001,0x00000a94,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x0000003c,0x00000810,0xf);
	perh_reg_wr(0x00000001,0x00000a70,0x00000601,0xf);
	perh_reg_wr(0x00000001,0x00000a48,0x00000001,0xf);
	perh_reg_wr(0x00000001,0x00000a58,0x00000001,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4600,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4640,0xf);
	prdata_js_monitor = (0x46105000 | (rl_2 & 0x3f));
	perh_reg_wr(0x00000001,0x00000000,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x0c000000,0xf);
	prdata_js_monitor = ((target_freq / 2) & 0xfff);
	perh_reg_wr(0x00000001,0x00000174,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c000000,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4640,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4640,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807406,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807406,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x02001300,0xf);
	perh_reg_wr(0x00000001,0x00000160,0x00001000,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c080000,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x02000300,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x02070300,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4640,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c080000,0xf);
	prdata_js_monitor = (0x46105000 | (rl_2 & 0x3f));
	perh_reg_wr(0x00000001,0x00000000,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x02070300,0xf);
	perh_reg_wr(0x00000001,0x00000008,0x20142001,0xf);
	perh_reg_wr(0x00000001,0x000000b8,0x00003000,0xf);
	perh_reg_wr(0x00000001,0x00000008,0x20142001,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4640,0xf);
	prdata_js_monitor = (0x46115000 | (rl_2 & 0x3f));
	perh_reg_wr(0x00000001,0x00000000,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000014,0x0083806f,0xf);
	perh_reg_wr(0x00000001,0x00000008,0x20142001,0xf);
	perh_reg_wr(0x00000001,0x000000b8,0x00003000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000015f,0xf);
	}else{

		perh_reg_wr(0x00000001,0x000000b0,0x2000015e,0xf);
	}
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f05e,0xf);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x00000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05e,0xf);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x20000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f05e,0xf);
	}
	prdata_js_monitor = (0x46115000 | (rl_2 & 0x3f));
	perh_reg_wr(0x00000001,0x00000000,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000a1c,0x0002080e,0xf);
	lpddr5_mrw(0x12,0x88);
	prdata_js_monitor = (wl_mr << 4);
	lpddr5_mrw(0x1,prdata_js_monitor);
	prdata_js_monitor = (rl_mr | (rl_mr << 4));
	lpddr5_mrw(0x2,prdata_js_monitor);
	if(target_freq <= 3200){
		lpddr5_mrw(0x3,0xd6);
	}else{
		lpddr5_mrw(0x3,0xc6);
	}
	lpddr5_mrw(0xa,0x0);
	lpddr5_mrw(0x14,0x2);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000000b4);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000000fc);
	perh_reg_wr(0x00000001,0x000000b8,0x00003000,0xf);
	if(target_freq <= 3200){
		prdata_js_monitor = 0x00200020 | (twckenl_wr & 0xf) | ((twckenl_rd_1 & 0xf) << 8) | ((twckpre_toggle_rd & 0xf) << 12) | ((twckpre_static & 0xf) << 16);
	}else{
		prdata_js_monitor = 0x00200020 | (twckenl_wr & 0xf) | ((twckenl_rd_2 & 0xf) << 8) | ((twckpre_toggle_rd & 0xf) << 12) | ((twckpre_static & 0xf) << 16);
	}
	perh_reg_wr(0x00000001,0x00000adc,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000af4,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807406,0xf);
//wait until ZQ_CON1.zq_done[0]
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000003cc);
	while(!((prdata_js_monitor & 0x00000001) == 0x00000001)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x000003cc);
		//while(1);
	}
//	printf("ZQ_CON1(0x010003cc).zq_done[0] = 1'b1 pass!!!");
	if((prdata_js_monitor & 0x00000004) == 0x00000000){
//		printf("ZQ_CON1(0x010003cc).zq_error[2] = 1'b0 pass!!!");
//		printf("ZQ_CON1(0x010003cc).zq_pmon[8:6],zq_nmon[5:3] is %x",prdata_js_monitor);
	}else{
//		printf("ZQ_CON1(0x010003cc).zq_error[2] = 1'b1 fail!!!");
		while(1);
	}
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x00000a70,0x00400601,0xf);
	perh_reg_wr(0x00000001,0x00000a20,0x00073b41,0xf);
	perh_reg_wr(0x00000001,0x00000a6c,0x00000002,0xf);
	perh_reg_wr(0x00000001,0x00000a6c,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000a6c,0x00000001,0xf);
	perh_reg_wr(0x00000001,0x00000a6c,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000a20,0x00073b40,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
	}
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f03f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f03e,0xf);
	}
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000010,0xfc7f9800,0xf);
	perh_reg_wr(0x00000001,0x0000001c,0x0300f00f,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4640,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4640,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003e0,0x00002121,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807406,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807406,0xf);
	perh_reg_wr(0x00000001,0x000003c8,0xff807404,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x02070300,0xf);
	perh_reg_wr(0x00000001,0x00000160,0x00001000,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c080000,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x02070300,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x02070300,0xf);
	perh_reg_wr(0x00000001,0x00000400,0x00180030,0xf);
	perh_reg_wr(0x00000001,0x00000400,0x00180033,0xf);
	perh_reg_wr(0x00000001,0x000000bc,0x00380400,0xf);
	perh_reg_wr(0x00000001,0x000000c0,0x00380400,0xf);
	perh_reg_wr(0x00000001,0x000000bc,0x03380400,0xf);
	perh_reg_wr(0x00000001,0x000000c0,0x03380400,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c080000,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c500000,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x07070300,0xf);
	perh_reg_wr(0x00000001,0x0000000c,0x0707b300,0xf);
	if(target_freq <= 4267){
		perh_reg_wr(0x00000001,0x0000000c,0x0707c300,0xf);
	}else{
		perh_reg_wr(0x00000001,0x0000000c,0x0707b300,0xf);
	}
}

void samsung_hwcal_cbt(unsigned int target_freq){
	unsigned int cbt_result_ca01vwmc;
	unsigned int cbt_result_ca23vwmc;
	unsigned int cbt_result_ca45vwmc;
	unsigned int cbt_result_ca6svwmc;
	unsigned int cbt_result_ca01vwml;
	unsigned int cbt_result_ca23vwml;
	unsigned int cbt_result_ca45vwml;
	unsigned int cbt_result_ca6svwml;
	unsigned int cbt_result_ca01vwmr;
	unsigned int cbt_result_ca23vwmr;
	unsigned int cbt_result_ca45vwmr;
	unsigned int cbt_result_ca6svwmr;
	unsigned int cbt_one_tck;
	perh_reg_wr(0x00000001,0x00000a70,0x00400601,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x02806300,0xf);
	perh_reg_wr(0x00000001,0x0000060c,0x80c80000,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c6640,0xf);
	perh_reg_wr(0x00000001,0x0000001c,0x0300f00c,0xf);
	perh_reg_wr(0x00000001,0x0000001c,0x0300c00c,0xf);
	perh_reg_wr(0x00000001,0x00000a20,0x00073b41,0xf);
	if(target_freq <= 3200){
		perh_reg_wr(0x00000001,0x00000004,0x581086c4,0xf);
	}else{
		perh_reg_wr(0x00000001,0x00000004,0x581006c4,0xf);
	}
	perh_reg_wr(0x00000001,0x00000600,0x0205e24d,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c501000,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c501000,0xf);
	perh_reg_wr(0x00000001,0x00000a78,0x00000100,0xf);
	perh_reg_wr(0x00000001,0x00000a78,0x00010100,0xf);
	perh_reg_wr(0x00000001,0x00000a68,0x00001000,0xf);
	perh_reg_wr(0x00000001,0x00000a68,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000600,0x0225e24d,0xf);
	perh_reg_wr(0x00000001,0x00000600,0x0225e24d,0xf);
	perh_reg_wr(0x00000001,0x00000600,0x0225e24d,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x22806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x2a806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0xaa806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x2a806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x6a806300,0xf);
	perh_reg_wr(0x00000001,0x00000a20,0x00073b40,0xf);
	perh_reg_wr(0x00000001,0x00000600,0x0223e24d,0xf);
	perh_reg_wr(0x00000001,0x0000060c,0x80c80001,0xf);
	perh_reg_wr(0x00000001,0x00000600,0x8223e24d,0xf);
	perh_reg_wr(0x00000001,0x0000008c,0x00200000,0xf);
	perh_reg_wr(0x00000001,0x00000090,0x00000020,0xf);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000600,0x0223e24d,0xf);
	perh_reg_wr(0x00000001,0x00000600,0x0223ea4d,0xf);
	perh_reg_wr(0x00000001,0x0000060c,0x80c80003,0xf);
//hw_cbt training result
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000bc4);
	while((prdata_js_monitor & 0x100002fe) != 0x100002fe){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000bc4);
	}
	perh_reg_wr(0x00000001,0x0000060c,0x80c80001,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000bb0);
//	printf("CMD_DESKEWC_CODE0(0x01000bb0).Ca1DeSkewCode[25:16],Ca0DeSkewCode[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000bb4);
//	printf("CMD_DESKEWC_CODE1(0x01000bb4).Ca3DeSkewCode[25:16],Ca2DeSkewCode[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000bb8);
//	printf("CMD_DESKEWC_CODE2(0x01000bb8).Ca5DeSkewCode[25:16],Ca4DeSkewCode[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000bbc);
//	printf("CMD_DESKEWC_CODE3(0x01000bbc).Ca6DeSkewCode[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000bc0);
//	printf("CMD_DESKEWC_CODE4(0x01000bc0).Cs1DeSkewCode[25:16],Cs0DeSkewCode[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000600);
	perh_reg_wr(0x00000001,0x00000600,0x8223ea4d,0xf);
	cbt_result_ca01vwml = perh_reg_rd(0x00000001,0x00000bc8);
//	printf("CMD_LEFT_CODE0(0x01000bc8).Ca0VWMLCode[12:0],Ca1VWMLCode[28:16] is 0x%x",prdata_js_monitor);
	cbt_result_ca23vwml = perh_reg_rd(0x00000001,0x00000bcc);
//	printf("CMD_LEFT_CODE1(0x01000bcc).Ca2VWMLCode[12:0],Ca3VWMLCode[28:16] is 0x%x",prdata_js_monitor);	
	cbt_result_ca45vwml = perh_reg_rd(0x00000001,0x00000bd0);
//	printf("CMD_LEFT_CODE2(0x01000bd0).Ca4VWMLCode[12:0],Ca5VWMLCode[28:16] is 0x%h",prdata_js_monitor);
	cbt_result_ca6svwml = perh_reg_rd(0x00000001,0x00000bd8);
//	printf("CMD_LEFT_CODE4(0x01000bd8).Ca6VWMLCode[12:0],CsVWMLFineCode[25:16] is 0x%x",prdata_js_monitor);
	cbt_result_ca01vwmr = perh_reg_rd(0x00000001,0x00000bdc);
//	printf("CMD_RIGHT_CODE0(0x01000bdc).Ca0VWMRCode[12:0],Ca1VWMRCode[28:16] is 0x%x",prdata_js_monitor);
	cbt_result_ca23vwmr = perh_reg_rd(0x00000001,0x00000be0);
//	printf("CMD_RIGHT_CODE1(0x01000be0).Ca2VWMRCode[12:0],Ca3VWMRCode[28:16] is 0x%x",prdata_js_monitor);
	cbt_result_ca45vwmr = perh_reg_rd(0x00000001,0x00000be4);
//	printf("CMD_RIGHT_CODE2(0x01000be4).Ca4VWMRCode[12:0],Ca5VWMRCode[28:16] is 0x%x",prdata_js_monitor);	
	cbt_result_ca6svwmr = perh_reg_rd(0x00000001,0x00000bec);
//printf("CMD_RIGHT_CODE4(0x01000bec).Ca6VWMRCode[12:0],CsVMRFineCode[25:16] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000afc);
//	printf("MDLL_CON2(0x01000afc).ctrl_lock_value_init[11:0] = 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000000b4);
	cbt_one_tck = (prdata_js_monitor & 0x000007ff) * 4;
//	printf("MDLL_CON1(0x010000b4).ctrl_lock_value[10:0] is 0x%x",prdata_js_monitor);
	cbt_result_ca01vwmc = ((cbt_result_ca01vwml + cbt_result_ca01vwmr)/2 & 0x03ff03ff);
	cbt_result_ca23vwmc = ((cbt_result_ca23vwml + cbt_result_ca23vwmr)/2 & 0x03ff03ff);
	cbt_result_ca45vwmc = ((cbt_result_ca45vwml + cbt_result_ca45vwmr)/2 & 0x03ff03ff);
	cbt_result_ca6svwmc = ((cbt_result_ca6svwml + cbt_result_ca6svwmr)/2 & 0x03ff03ff);
//cbt_result_ca01vwmc[9:0] = (cbt_result_ca01vwml[12:0] + cbt_result_ca01vwmr[12:0])/2;
//cbt_result_ca01vwmc[25:16] = (cbt_result_ca01vwml[28:16] + cbt_result_ca01vwmr[28:16])/2;
//cbt_result_ca23vwmc[9:0] = (cbt_result_ca23vwml[12:0] + cbt_result_ca23vwmr[12:0])/2;
//cbt_result_ca23vwmc[25:16] = (cbt_result_ca23vwml[28:16] + cbt_result_ca23vwmr[28:16])/2;
//cbt_result_ca45vwmc[9:0] = (cbt_result_ca45vwml[12:0] + cbt_result_ca45vwmr[12:0])/2;
//cbt_result_ca45vwmc[25:16] = (cbt_result_ca45vwml[28:16] + cbt_result_ca45vwmr[28:16])/2;
//cbt_result_ca6svwmc[9:0] = (cbt_result_ca6svwml[12:0] + cbt_result_ca6svwmr[12:0])/2;
	perh_reg_wr(0x00000001,0x0000007c,cbt_result_ca01vwmc,0xf);
//	printf("write CA_DESKEW_CON0(0x0100007c).CA0DeSkewCode[9:0],CA1DeSkewCode[25:16] is 0x%x",cbt_result_ca01vwmc);
	perh_reg_wr(0x00000001,0x00000080,cbt_result_ca23vwmc,0xf);
//	printf("write CA_DESKEW_CON1(0x01000080).CA2DeSkewCode[9:0],CA3DeSkewCode[25:16] is 0x%x",cbt_result_ca23vwmc);
	perh_reg_wr(0x00000001,0x00000084,cbt_result_ca45vwmc,0xf);
//	printf("write CA_DESKEW_CON2(0x01000084).CA4DeSkewCode[9:0],CA5DeSkewCode[25:16] is 0x%x",cbt_result_ca45vwmc);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000008c);
//	printf("write CA_DESKEW_CON4(0x0100008c).CKDeSkewCode[25:16],CA6DeSkewCode[9:0] is 0x%",prdata_js_monitor);
	prdata_js_monitor = (prdata_js_monitor & 0xfffffc00);
	prdata_js_monitor = ((cbt_result_ca6svwmc & 0x000003ff) | prdata_js_monitor);
	perh_reg_wr(0x00000001,0x0000008c,prdata_js_monitor,0xf);
//	printf("write CA_DESKEW_CON4(0x0100008c).CKDeSkewCode[25:16],CA6DeSkewCode[9:0] is 0x%x",prdata_js_monitor);
	if((cbt_result_ca6svwml & 0x03ff0000) <= (cbt_result_ca6svwmr & 0x03ff0000)){
		cbt_result_ca6svwmc = (cbt_result_ca6svwmc & 0xfc00ffff) | (((cbt_result_ca6svwmr & 0x03ff0000) - (cbt_result_ca6svwml & 0x03ff0000))/2);
	}else{
		perh_reg_wr(0x00000001,0x0000060c,0x80c8000d,0xf);
		cbt_result_ca6svwmc = (cbt_result_ca6svwmc & 0xfc00ffff) | ((cbt_one_tck - (cbt_result_ca6svwml & 0x03ff0000) + (cbt_result_ca6svwmr & 0x03ff0000))/2);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000090);
//	printf("CA_DESKEW_CON5(0x01000090).CS0DeSkewCode[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = (prdata_js_monitor & 0xfc00fc00) | ((cbt_result_ca6svwmc & 0x03ff0000) >> 16) | cbt_result_ca6svwmc;
	perh_reg_wr(0x00000001,0x00000090,prdata_js_monitor,0xf);
//	printf("write CA_DESKEW_CON5(0x01000090).CS0DeSkewCode[9:0] is 0x%x",prdata_js_monitor);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000600,0x0223ea4d,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x4a806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x0a806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x02806300,0xf);
	perh_reg_wr(0x00000001,0x00000a20,0x00073b41,0xf);
	perh_reg_wr(0x00000001,0x00000a78,0x00010100,0xf);
	perh_reg_wr(0x00000001,0x00000a78,0x00000100,0xf);
	perh_reg_wr(0x00000001,0x00000a68,0x00001000,0xf);
	perh_reg_wr(0x00000001,0x00000a68,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000600,0x0203ea4d,0xf);
	perh_reg_wr(0x00000001,0x00001048,0x2c500000,0xf);
	perh_reg_wr(0x00000001,0x00000600,0x0003ea4d,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x12806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x02806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x42806300,0xf);
	perh_reg_wr(0x00000001,0x00000608,0x02806300,0xf);
	if(target_freq <= 3200){
		perh_reg_wr(0x00000001,0x00000004,0x581086c0,0xf);
	}else{
		perh_reg_wr(0x00000001,0x00000004,0x581006c0,0xf);
	}
	perh_reg_wr(0x00000001,0x00000a20,0x00073b40,0xf);
	perh_reg_wr(0x00000001,0x0000001c,0x0300c00f,0xf);
	perh_reg_wr(0x00000001,0x0000001c,0x0300f00f,0xf);
	perh_reg_wr(0x00000001,0x00000018,0x008c4640,0xf);
}	

void samsung_wrlvl(unsigned int target_freq){
	perh_reg_wr(0x00000001,0x00000a2c,0x025a01c0,0xf);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x20000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000a24,0x00000008,0xf);
	perh_reg_wr(0x00000001,0x00000a24,0x00000108,0xf);
	perh_reg_wr(0x00000001,0x00000a24,0x00000109,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000a24);
	while(!((prdata_js_monitor & 0x00000002) == 0x00000002)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000a24);
		//while(1);
	}
//	printf("afer set 0x00000109,SCHD_TRAIN_CON0(0x01000a24).phy_train_done[1] = 1'b1 pass!!!");	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000004);
	while(((prdata_js_monitor & 0x00010000) != 0x00010000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000004);
		//while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000000e4);
	if(((prdata_js_monitor & 0x00030000) == 0x00000000)){
//		printf("CAL_WRLVL_STAT(0x010000e4).wrlvl_fail_status_rank0[17:16]  = 2'b00 pass!!!");
	}else{
//		printf("CAL_WRLVL_STAT(0x010000e4).wrlvl_fail_status_rank0[17:16] != 2'b00 fail!!!");
		while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000060);
//	printf("WR_LVL_CODE0(0x01000060).WrlvlCode1_cs0[25:16],WrlvlCode0_cs0[9:0] is 0x%x",prdata_js_monitor);
	perh_reg_wr(0x00000001,0x00000a24,0x00000000,0xf);
//	printf("WCK_CYC_CODE(0x01000ff8).ctrl_shift_wck_ds1_cs1[14:12],ctrl_shift_wck_ds0_cs1[10:8],ctrl_shift_wck_ds1_cs0[6:4],ctrl_shift_wck_ds0_cs0[2:0] is 0x%x",prdata_js_monitor);
	perh_reg_wr(0x00000001,0x00000a24,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
	}

}

void samsung_rd(unsigned int target_freq){
	lpddr5_mrw(0x12,0x0);
	perh_reg_wr(0x00000001,0x00000014,0x0083806f,0xf);
	perh_reg_wr(0x00000001,0x00000a7c,0x5555eeee,0xf);
	perh_reg_wr(0x00000001,0x00000a7c,0x5555eeee,0xf);
	perh_reg_wr(0x00000001,0x00000a7c,0x5555eeee,0xf);
	perh_reg_wr(0x00000001,0x00000a7c,0x5555eeee,0xf);
	perh_reg_wr(0x00000001,0x000000ac,0xeeee5555,0xf);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x20000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000a24,0x00000020,0xf);
	perh_reg_wr(0x00000001,0x00000a24,0x00001020,0xf);
	perh_reg_wr(0x00000001,0x00000a24,0x00001021,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000a24);
	while(!((prdata_js_monitor & 0x00000002) == 0x00000002)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000a24);
		//while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000000e8);
//	printf("CAL_FAIL_STAT0(0x010000e8).rdtrn_overflow_cs1[29:28],rdtrn_overflow_cs0[27:26],dq_fail_status1[15:8],dq_fail_status0[7:0] is 0x%x",prdata_js_monitor);
	if(((prdata_js_monitor & 0xffffffff) == 0x00000000)){
//		printf(" CAL_FAIL_STAT0(0x010000e8).rdtrn_overflow_cs1[29:28],rdtrn_overflow_cs0[27:26],dq_fail_status1[15:8],dq_fail_status0[7:0] = 0x00000000 pass!!!");
	}else{
//		printf(" CAL_FAIL_STAT0(0x010000e8).rdtrn_overflow_cs1[29:28],rdtrn_overflow_cs0[27:26],dq_fail_status1[15:8],dq_fail_status0[7:0] = 0x00000000 fail!!!");
		while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000000ec);
//	printf("CAL_FAIL_STAT1(0x010000ec).dm_fail_status[1:0] is 0x%x",prdata_js_monitor);	
	if(((prdata_js_monitor & 0xffffffff) == 0x00000000)){
//		printf("CAL_FAIL_STAT1(0x010000ec).dm_fail_status[1:0] = 0x00000000 pass!!!");
	}else{
//		printf("CAL_FAIL_STAT1(0x010000ec).dm_fail_status[1:0] = 0x00000000 fail!!!");
		while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000100);
//	printf("CAL_RD_VWMC0(0x01000100).rd_vwmc1[25:16],rd_vwmc0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000110);
//	printf("CAL_RD_VWML0(0x01000110).rd_vwml1[25:16],rd_vwml0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000120);
//	printf("CAL_RD_VWMR0(0x01000120).rd_vwmr1[25:16],rd_vwmr0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000018c);
//	printf("RD_DESKEW_CENTER_CS0_CON_DM(0x0100018c).RDDMDeskewC1_cs0[25:16],RDDMDeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000190);
//	printf("RD_DESKEW_CENTER_CS0_CON0(0x01000190).RD0DeskewC1_cs0[25:16],RD0DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000019c);
//	printf("RD_DESKEW_CENTER_CS0_CON1(0x0100019c).RD1DeskewC1_cs0[25:16],RD1DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000001a8);
//	printf("RD_DESKEW_CENTER_CS0_CON2(0x010001a8).RD2DeskewC1_cs0[25:16],RD2DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000001b4);
//	printf("RD_DESKEW_CENTER_CS0_CON3(0x010001b4).RD3DeskewC1_cs0[25:16],RD3DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000001c0);
//	printf("RD_DESKEW_CENTER_CS0_CON4(0x010001c0).RD4DeskewC1_cs0[25:16],RD4DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000001cc);
//	printf("RD_DESKEW_CENTER_CS0_CON5(0x010001cc).RD5DeskewC1_cs0[25:16],RD5DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000001d8);
//	printf("RD_DESKEW_CENTER_CS0_CON6(0x010001d8).RD6DeskewC1_cs0[25:16],RD6DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000001e4);
//	printf("RD_DESKEW_CENTER_CS0_CON7(0x010001e4).RD7DeskewC1_cs0[25:16],RD7DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000610);
//	printf("RD_DESKEW_LEFT_CS0_CON_DM(0x01000610).RDDMDeskewLeft1_cs0[25:16],RDDMDeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000614);
//	printf("RD_DESKEW_LEFT_CS0_CON0(0x01000614).RD0DeskewLeft1_cs0[25:16],RD0DeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000620);
//	printf("RD_DESKEW_LEFT_CS0_CON1(0x01000620).RD1DeskewLeft1_cs0[25:16],RD1DeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000062c);
//	printf("RD_DESKEW_LEFT_CS0_CON2(0x0100062c).RD2DeskewLeft1_cs0[25:16],RD2DeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000638);
//	printf("RD_DESKEW_LEFT_CS0_CON3(0x01000638).RD3DeskewLeft1_cs0[25:16],RD3DeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000644);
//	printf("RD_DESKEW_LEFT_CS0_CON4(0x01000644).RD4DeskewLeft1_cs0[25:16],RD4DeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000650);
//	printf("RD_DESKEW_LEFT_CS0_CON5(0x01000650).RD5DeskewLeft1_cs0[25:16],RD5DeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000065c);
//	printf("RD_DESKEW_LEFT_CS0_CON6(0x0100065c).RD6DeskewLeft1_cs0[25:16],RD6DeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000668);
//	printf("RD_DESKEW_LEFT_CS0_CON7(0x01000668).RD7DeskewLeft1_cs0[25:16],RD7DeskewLeft0_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000580);
//	printf("RD_DQS_VWMC_CS0_CON0(0x01000580).RDDQSVWMC1_cs0[24:16],RDDQSVWMC0_cs0[8:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000574);
//	printf("RD_DQS_VWML_CS0_CON0(0x01000574).RDDQSVWML1_cs0[24:16],RDDQSVWML0_cs0[8:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001130);
//	printf("CTRL_COM_OFFSET_DQ_CTRL0(0x01001130).CTRL_COM_OFFSET_DQ3_DS0[30:24],CTRL_COM_OFFSET_DQ2_DS0[22:16],CTRL_COM_OFFSET_DQ1_DS0[14:8],CTRL_COM_OFFSET_DQ0_DS0[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001134);
//	printf("CTRL_COM_OFFSET_DQ_CTRL1(0x01001134).CTRL_COM_OFFSET_DQ7_DS0[30:24],CTRL_COM_OFFSET_DQ6_DS0[22:16],CTRL_COM_OFFSET_DQ5_DS0[14:8],CTRL_COM_OFFSET_DQ4_DS0[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001138);
//	printf("CTRL_COM_OFFSET_DQ_CTRL2(0x01001138).CTRL_COM_OFFSET_DM8_DS0[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000113c);
//	printf("CTRL_COM_OFFSET_DQ_CTRL3(0x0100113c).CTRL_COM_OFFSET_DQ3_DS1[30:24],CTRL_COM_OFFSET_DQ2_DS1[22:16],CTRL_COM_OFFSET_DQ1_DS1[14:8],CTRL_COM_OFFSET_DQ0_DS1[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001140);
//	printf("CTRL_COM_OFFSET_DQ_CTRL4(0x01001140).CTRL_COM_OFFSET_DQ7_DS1[30:24],CTRL_COM_OFFSET_DQ6_DS1[22:16],CTRL_COM_OFFSET_DQ5_DS1[14:8],CTRL_COM_OFFSET_DQ4_DS1[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001144);
//	printf("CTRL_COM_OFFSET_DQ_CTRL5(0x01001144).CTRL_COM_OFFSET_DM8_DS1[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001148);
//	printf("CTRL_COM_OFFSET_DFE_DQ_CTRL0(0x01001148).CTRL_COM_OFFSET_DFE_DQ3_DS0[30:24],CTRL_COM_OFFSET_DFE_DQ2_DS0[22:16],CTRL_COM_OFFSET_DFE_DQ1_DS0[14:8],CTRL_COM_OFFSET_DFE_DQ0_DS0[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000114c);
//	printf("CTRL_COM_OFFSET_DFE_DQ_CTRL1(0x0100114c).CTRL_COM_OFFSET_DFE_DQ7_DS0[30:24],CTRL_COM_OFFSET_DFE_DQ6_DS0[22:16],CTRL_COM_OFFSET_DFE_DQ5_DS0[14:8],CTRL_COM_OFFSET_DFE_DQ4_DS0[6:0 is 0x%x]",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001150);
//	printf("CTRL_COM_OFFSET_DFE_DQ_CTRL2(0x01001150).CTRL_COM_OFFSET_DFE_DM8_DS0[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001154);
//	printf("CTRL_COM_OFFSET_DFE_DQ_CTRL3(0x01001154).CTRL_COM_OFFSET_DFE_DQ3_DS1[30:24],CTRL_COM_OFFSET_DFE_DQ2_DS1[22:16],CTRL_COM_OFFSET_DFE_DQ1_DS1[14:8],CTRL_COM_OFFSET_DFE_DQ0_DS1[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00001158);
//	printf("CTRL_COM_OFFSET_DFE_DQ_CTRL4(0x01001158).CTRL_COM_OFFSET_DFE_DQ7_DS1[30:24],CTRL_COM_OFFSET_DFE_DQ6_DS1[22:16],CTRL_COM_OFFSET_DFE_DQ5_DS1[14:8],CTRL_COM_OFFSET_DFE_DQ4_DS1[6:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000115c);
//	printf("CTRL_COM_OFFSET_DFE_DQ_CTRL5(0x0100115c).CTRL_COM_OFFSET_DFE_DM8_ds1[6:0] is 0x%x",prdata_js_monitor);
	perh_reg_wr(0x00000001,0x00000a24,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
	}
}

void samsung_wr(unsigned int target_freq){
	lpddr5_mrw(0x12,0x0);
	perh_reg_wr(0x00000001,0x00000098,0x55aa55aa,0xf);
	perh_reg_wr(0x00000001,0x0000009c,0x00ff00ff,0xf);
	perh_reg_wr(0x00000001,0x000000a0,0x55aa55aa,0xf);
	perh_reg_wr(0x00000001,0x000000a4,0x00ff00ff,0xf);
	perh_reg_wr(0x00000001,0x0000093c,0x21002322,0xf);
	perh_reg_wr(0x00000001,0x00000940,0x25442766,0xf);
	perh_reg_wr(0x00000001,0x00000944,0x29882baa,0xf);
	perh_reg_wr(0x00000001,0x00000948,0x2dcc2fee,0xf);
	perh_reg_wr(0x00000001,0x0000094c,0x31003322,0xf);
	perh_reg_wr(0x00000001,0x00000950,0x35443766,0xf);
	perh_reg_wr(0x00000001,0x00000954,0x39883baa,0xf);
	perh_reg_wr(0x00000001,0x00000958,0x3dcc3fee,0xf);
	perh_reg_wr(0x00000001,0x0000095c,0x41004322,0xf);
	perh_reg_wr(0x00000001,0x00000960,0x45444766,0xf);
	perh_reg_wr(0x00000001,0x00000964,0x49884baa,0xf);
	perh_reg_wr(0x00000001,0x00000968,0x4dcc4fee,0xf);
	perh_reg_wr(0x00000001,0x000000a8,0x00005555,0xf);
	perh_reg_wr(0x00000001,0x0000096c,0x0000aaaa,0xf);
	perh_reg_wr(0x00000001,0x00000970,0x0000ffff,0xf);
	perh_reg_wr(0x00000001,0x00000974,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000008,0x20142001,0xf);
	perh_reg_wr(0x00000001,0x00000014,0x0083806f,0xf);
	perh_reg_wr(0x00000001,0x00000a30,0x640d1008,0xf);
	perh_reg_wr(0x00000001,0x00000a30,0x67cd1008,0xf);
	perh_reg_wr(0x00000001,0x00000a40,0x67cd1008,0xf);
	perh_reg_wr(0x00000001,0x00000a50,0x67cd1008,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x20000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000a24,0x00000040,0xf);
	perh_reg_wr(0x00000001,0x00000a24,0x00004040,0xf);
	perh_reg_wr(0x00000001,0x00000a24,0x00004041,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000a24);
	while(!((prdata_js_monitor & 0x00000002) == 0x00000002)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000a24);
		//while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000000e8);
//	printf("CAL_FAIL_STAT0(0x010000e8).dq_fail_status1[15:8],dq_fail_status0[7:0] is 0x%x",prdata_js_monitor);
	if(((prdata_js_monitor & 0xffffffff) == 0x00000000)){
//		printf(" CAL_FAIL_STAT0(0x010000e8).dq_fail_status1[15:8],dq_fail_status0[7:0] = 0x00000000 pass!!!");
	}else{
//		printf(" CAL_FAIL_STAT0(0x010000e8).dq_fail_status1[15:8],dq_fail_status0[7:0] = 0x00000000 fail!!!");
		while(1);
	}	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000000ec);
//	printf("CAL_FAIL_STAT1(0x010000ec).wrtrn_overflow_cs1[17:16] is 0x%x",prdata_js_monitor);	
	if(((prdata_js_monitor & 0xffffffff) == 0x00000000)){
//		printf(" CAL_FAIL_STAT1(0x010000ec).wrtrn_overflow_cs1[17:16] = 0x00000000 pass!!!");
	}else{
//		printf(" CAL_FAIL_STAT1(0x010000ec).wrtrn_overflow_cs1[17:16] = 0x00000000  fail!!!");
		while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000130);
//	printf("CAL_WR_VWMC0(0x01000130).wr_vwmc1[26:16],wr_vwmc0[10:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000140);
//	printf("CAL_WR_VWML0(0x01000140).wr_vwml1[26:16],wr_vwml0[10:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000150);
//	printf("CAL_WR_VWMR0(0x01000150).wr_vwmr1[26:16],wr_vwmr0[10:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000001f0);
//	printf("WR_DESKEWC_CS0_CON0(0x010001f0).WR0DeskewC1_cs0[25:16],WR0DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000001fc);
//	printf("WR_DESKEWC_CS0_CON1(0x010001fc).WR1DeskewC1_cs0[25:16],WR1DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000208);
//	printf("WR_DESKEWC_CS0_CON2(0x01000208).WR2DeskewC1_cs0[25:16],WR2DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000214);
//	printf("WR_DESKEWC_CS0_CON3(0x01000214).WR3DeskewC1_cs0[25:16],WR3DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000220);
//	printf("WR_DESKEWC_CS0_CON4(0x01000220).WR4DeskewC1_cs0[25:16],WR4DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000022c);
//	printf("WR_DESKEWC_CS0_CON5(0x0100022c).WR5DeskewC1_cs0[25:16],WR5DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000238);
//	printf("WR_DESKEWC_CS0_CON6(0x01000238).WR6DeskewC1_cs0[25:16],WR6DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000244);
//	printf("WR_DESKEWC_CS0_CON7(0x01000244).WR7DeskewC1_cs0[25:16],WR7DeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000250);
//	printf("DM_DWSKEWC_CS0_CON0(0x01000250).DMDeskewC1_cs0.DMDeskewC1_cs0[25:16],DMDeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000254);
//	printf("ECC_DESKEWC_CS0_CON0(0x01000254).ECCDeskewC1_cs0[25:16],ECCDeskewC0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000490);
//	printf("WR_DESKEWL_CS0_CON0(0x01000490).WR0DdeskewL1_cs0[25:16],WR0DeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000049c);
//	printf("WR_DESKEWL_CS0_CON1(0x0100049c).WR1DdeskewL1_cs0[25:16],WR1DeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000004a8);
//	printf("WR_DESKEWL_CS0_CON2(0x010004a8).WR2DdeskewL1_cs0[25:16],WR2DeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000004b4);
//	printf("WR_DESKEWL_CS0_CON3(0x010004b4).WR3DdeskewL1_cs0[25:16],WR3DeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000004c0);
//	printf("WR_DESKEWL_CS0_CON4(0x010004c0).WR4DdeskewL1_cs0[25:16],WR4DeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000004cc);
//	printf("WR_DESKEWL_CS0_CON5(0x010004cc).WR5DdeskewL1_cs0[25:16],WR5DeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000004d8);
//	printf("WR_DESKEWL_CS0_CON6(0x010004d8).WR6DdeskewL1_cs0[25:16],WR6DeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000004e4);
//	printf("WR_DESKEWL_CS0_CON7(0x010004e4).WR7DdeskewL1_cs0[25:16],WR7DeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000004f0);
//	printf("DM_DESKEWL_CS0_CON0(0x010004f0).DMDeskewL1_cs0[25:16],DMDeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x000004f4);
//	printf("ECC_DESKEWL_CS0_CON0(0x010004f4).ECCDeskewL1_cs0[25:16],ECCDeskewL0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000dc0);
//	printf("R_BYTE0_CYC_CS0_CODE(0x01000dc0).ctrl_shift_ecc_ds0_cs0[29:27],ctrl_shift_dm_ds0_cs0[26:24],ctrl_shift_dq7_ds0_cs0[23:21],ctrl_shift_dq6_ds0_cs0[20:18],ctrl_shift_dq5_ds0_cs0[17:15],ctrl_shift_dq4_ds0_cs0[14:12],ctrl_shift_dq3_ds0_cs0[11:9],ctrl_shift_dq2_ds0_cs0[8:6],ctrl_shift_dq1_ds0_cs0[5:3],ctrl_shift_dq0_ds0_cs0[2:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000dc4);
//	printf("WR_BYTE1_CYC_CS0_CODE(0x01000dc4).ctrl_shift_ecc_ds1_cs0[29:27],ctrl_shift_dm_ds1_cs0[26:24],ctrl_shift_dq7_ds1_cs0[23:21],ctrl_shift_dq6_ds1_cs0[20:18],ctrl_shift_dq5_ds1_cs0[17:15],ctrl_shift_dq4_ds1_cs0[14:12],ctrl_shift_dq3_ds1_cs0[11:9],ctrl_shift_dq2_ds1_cs0[8:6],ctrl_shift_dq1_ds1_cs0[5:3],ctrl_shift_dq0_ds1_cs0[2:0] is 0x%x",prdata_js_monitor);
	perh_reg_wr(0x00000001,0x00000a24,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
	}

}

void samsung_prbs_rd(unsigned int target_freq){
	lpddr5_freq_wl_rl_trans(target_freq);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x20000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05e,0xf);
	}
	if(target_freq <= 3200){
		perh_reg_wr(0x00000001,0x00000004,0x5c1086c0,0xf);
	}else{
		perh_reg_wr(0x00000001,0x00000004,0x5c1006c0,0xf);
	}
	perh_reg_wr(0x00000001,0x00000a24,0x00005100,0xf);
	prdata_js_monitor = (0x008200c0 | (wl & 0x3f));
	perh_reg_wr(0x00000001,0x00001054,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000a28,0x0000000f,0xf);
	perh_reg_wr(0x00000001,0x00000a24,0x00005101,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000688);
	prdata_js_monitor = ((prdata_js_monitor & 0xff03ffff) | ((wl & 0x3f) << 18));
	perh_reg_wr(0x00000001,0x00000688,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000684,0x00040002,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000684);
	while(!(prdata_js_monitor == 0x00040003)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000684);
		//while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x0000068c);
//	printf("PRBS_CON2(0x0100068c).prbs_rd_vwml_fail1[29:28],prbs_rd_vwmr_fial1[27:26],prbs_rd_vwml_fail0[13:12],prbs_rd_vwmr_fail0[11:10] is 0x%x",prdata_js_monitor);	
	if(((prdata_js_monitor & 0xffffffff) == 0x00000000)){
//		printf(" PRBS_CON2(0x0100068c).prbs_rd_vwml_fail1[29:28],prbs_rd_vwmr_fial1[27:26],prbs_rd_vwml_fail0[13:12],prbs_rd_vwmr_fail0[11:10] = 0x00000000 pass!!!");
	}else{
//		printf(" PRBS_CON2(0x0100068c).prbs_rd_vwml_fail1[29:28],prbs_rd_vwmr_fial1[27:26],prbs_rd_vwml_fail0[13:12],prbs_rd_vwmr_fail0[11:10] = 0x00000000 fail!!!");
		while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000d04);
//	printf("PRBS_READ_FAIL(0x01000d04).prbs_fail_read_dq_byte1[24:17],prbs_fail_read_dmi_byte1[16],prbs_fail_read_dq_byte0[8:1],prbs_fail_read_dmi_byte0[0] is 0x%x",prdata_js_monitor);
	if(((prdata_js_monitor & 0xffffffff) == 0x00000000)){
//		printf(" PRBS_READ_FAIL(0x01000d04).prbs_fail_read_dq_byte1[24:17],prbs_fail_read_dmi_byte1[16],prbs_fail_read_dq_byte0[8:1],prbs_fail_read_dmi_byte0[0] = 0x00000000 pass!!!");
	}else{
//		printf(" PRBS_READ_FAIL(0x01000d04).prbs_fail_read_dq_byte1[24:17],prbs_fail_read_dmi_byte1[16],prbs_fail_read_dq_byte0[8:1],prbs_fail_read_dmi_byte0[0] = 0x00000000 fail!!!");
		while(1);
	}
//for rank0, check PRBS_OFFSETR*_CS0 for PRBS read training result
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c74);
//	printf("PRBS_OFFSETR0_CS0(0x01000c74).prbs_offsetr_dmi_byte0_cs0[29:20],prbs_offsetr_dq0_byte0_cs0[19:10],prbs_offsetr_dq1_byte0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c78);
//	printf("PRBS_OFFSETR1_CS0(0x01000c78).prbs_offsetr_dq2_byte0_cs0[29:20],prbs_offsetr_dq3_byte0_cs0[19:10],prbs_offsetr_dq4_byte0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c7c);
//	printf("PRBS_OFFSETR2_CS0(0x01000c7c).prbs_offsetr_dq5_byte0_cs0[29:20],prbs_offsetr_dq3_byte0_cs0[19:10],prbs_offsetr_dq7_byte0_cs0[9:0] is 0x%x",)prdata_js_monitor;	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c80);
//	printf("PRBS_OFFSETR3_CS0(0x01000c80).prbs_offsetr_dmi_byte1_cs0[29:20],prbs_offsetr_dq0_byte1_cs0[19:10],prbs_offsetr_dq1_byte1_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c84);
//	printf("PRBS_OFFSETR4_CS0(0x01000c84).prbs_offsetr_dq2_byte1_cs0[29:20],prbs_offsetr_dq3_byte1_cs0[19:10],prbs_offsetr_dq4_byte1_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c88);
//	printf("PRBS_OFFSETR5_CS0(0x01000c88).prbs_offsetr_dq5_byte1_cs0[29:20],prbs_offsetr_dq3_byte1_cs0[19:10],prbs_offsetr_dq7_byte1_cs0[9:0] is 0x%x",prdata_js_monitor);
	perh_reg_wr(0x00000001,0x00000a24,0x00000000,0xf);
	if(target_freq <= 3200){
		perh_reg_wr(0x00000001,0x00000004,0x581086c0,0xf);
	}else{
		perh_reg_wr(0x00000001,0x00000004,0x581006c0,0xf);
	}
	prdata_js_monitor = (0x008000c0 | (wl & 0x3f));
	perh_reg_wr(0x00000001,0x00001054,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000684,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
	}
}

void samsung_prbs_wr(unsigned int target_freq){
	lpddr5_freq_wl_rl_trans(target_freq);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x20000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f05e,0xf);
	}
	if(target_freq <= 3200){
		perh_reg_wr(0x00000001,0x00000004,0x5c1086c0,0xf);
	}else{
		perh_reg_wr(0x00000001,0x00000004,0x5c1006c0,0xf);
	}
	perh_reg_wr(0x00000001,0x00000a24,0x00005100,0xf);
	prdata_js_monitor = (0x008200c0 | (wl & 0x3f));
	perh_reg_wr(0x00000001,0x00001054,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000a28,0x0000000f,0xf);
	perh_reg_wr(0x00000001,0x00000a24,0x00005101,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000688);
	prdata_js_monitor = ((prdata_js_monitor & 0xff03ffff) | ((wl & 0x3f) << 18));
	perh_reg_wr(0x00000001,0x00000688,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000684,0x00090004,0xf);
	while(!(prdata_js_monitor == 0x00090005)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000684);
		//while(1);
	}
//check if PRBS_CON2.prbs_rd_vwm*_fail*,PRBS_CON3.prbs_wr_vwm*_fail*, PRBS_READ_FAIL and PRBS_WRITE_FAIL = 0x0
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000690);
	if(((prdata_js_monitor & 0xffffffff) == 0x00000000)){
//		printf(" PRBS_CON3(0x01000690).prbs_fail_byte1[30],prbs_wr_vwml_fail1[29:28],prbs_wr_vwmr_fail1[27:26],prbs_fail_byte0[14],prbs_wr_vwml_fail[13:12],prbs_wr_vwmr_fail[11:10] = 0x00000000 pass!!!");
	}else{
//		printf(" PRBS_CON3(0x01000690).prbs_fail_byte1[30],prbs_wr_vwml_fail1[29:28],prbs_wr_vwmr_fail1[27:26],prbs_fail_byte0[14],prbs_wr_vwml_fail[13:12],prbs_wr_vwmr_fail[11:10] = 0x00000000 fail!!!");
		while(1);
	}
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000d08);
	if(((prdata_js_monitor & 0xffffffff) == 0x00000000)){
//		printf(" PRBS_WRITE_FAIL(0x01000d08).prbs_fail_write_dq_byte1[24:17],prbs_fail_write_dmi_byte1[16],prbs_fail_write_dq_byte0[8:1],prbs_fail_write_dmi_byte0[0] = 0x00000000 pass!!!");
	}else{
//		printf(" PRBS_WRITE_FAIL(0x01000d08).prbs_fail_write_dq_byte1[24:17],prbs_fail_write_dmi_byte1[16],prbs_fail_write_dq_byte0[8:1],prbs_fail_write_dmi_byte0[0] = 0x00000000 fail!!!");
		while(1);
	}
//	printf("PRBS_WRITE_FAIL(0x01000d08).prbs_fail_write_dq_byte1[24:17],prbs_fail_write_dmi_byte1[16],prbs_fail_write_dq_byte0[8:1],prbs_fail_write_dmi_byte0[0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c8c);
//	printf("PRBS_OFFSETW0_CS0(0x01000c8c).prbs_offsetw_dmi_byte0_cs0[29:20],prbs_offsetw_dq0_byte0_cs0[19:10],prbs_offsetw_dq1_byte0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c90);
//	printf("PRBS_OFFSETW1_CS0(0x01000c90).prbs_offsetw_dq2_byte0_cs0[29:20],prbs_offsetw_dq3_byte0_cs0[19:10],prbs_offsetw_dq4_byte0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c94);
//	printf("PRBS_OFFSETW2_CS0(0x01000c94).prbs_offsetw_dq5_byte0_cs0[29:20],prbs_offsetw_dq6_byte0_cs0[19:10],prbs_offsetw_dq7_byte0_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c98);
//	printf("PRBS_OFFSETW3_CS0(0x01000c98).prbs_offsetw_dmi_byte1_cs0[29:20],prbs_offsetw_dq0_byte1_cs0[19:10],prbs_offsetw_dq1_byte1_cs0[9:0] is 0x%x",prdata_js_monitor);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000c9c);
//	printf("PRBS_OFFSETW4_CS0(0x01000c9c).prbs_offsetw_dq2_byte1_cs0[29:20],prbs_offsetw_dq3_byte1_cs0[19:10],prbs_offsetw_dq4_byte1_cs0[9:0] is 0x%x",prdata_js_monitor);	
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000ca0);
//	printf("PRBS_OFFSETW5_CS0(0x01000ca0).prbs_offsetw_dq5_byte1_cs0[29:20],prbs_offsetw_dq6_byte1_cs0[19:10],prbs_offsetw_dq7_byte1_cs0[9:0] is 0x%x",prdata_js_monitor);
	perh_reg_wr(0x00000001,0x00000a24,0x00000000,0xf);
	if(target_freq <= 3200){
		perh_reg_wr(0x00000001,0x00000004,0x581086c0,0xf);
	}else{
		perh_reg_wr(0x00000001,0x00000004,0x581006c0,0xf);
	}
	prdata_js_monitor = (0x008000c0 | (wl & 0x3f));
	perh_reg_wr(0x00000001,0x00001054,prdata_js_monitor,0xf);
	perh_reg_wr(0x00000001,0x00000684,0x00000000,0xf);
	perh_reg_wr(0x00000001,0x00000050,0x20000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2080f07e,0xf);
	}
	perh_reg_wr(0x00000001,0x00000050,0x21000000,0xf);
	prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
	while(!(prdata_js_monitor == 0x2f000000)){
		prdata_js_monitor = perh_reg_rd(0x00000001,0x00000050);
		//while(1);
	}
	perh_reg_wr(0x00000001,0x00000050,0x2e000000,0xf);
	if(target_freq <= 1067){
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07f,0xf);
	}else{
		perh_reg_wr(0x00000001,0x000000b0,0x2000f07e,0xf);
	}
}
